package hottargui.config;

import java.util.Map;

import hottargui.framework.*;
import hottargui.standard.*;


public class GammaNewGameState implements TarguiState {
	
	Game gameContext;
	boolean dieThrown = false;
	boolean gameStarted = false;
	GammaNewGameState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.newGame;
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		if(gameStarted == false)
		{
			GameData gd = gameContext.getGameObjectServer().getGameData();
			// Setup board
			gameContext.getGameObjectServer().getBoardStrategy().setupBoard(gd);
			System.out.println("BoardSetup");
			
			Map<PlayerColor, Player> players = gd.getPlayers();
	
			// setup players
			Player redPlayer = new StandardPlayer(PlayerColor.Red);
			((StandardPlayer)redPlayer).setSettlement(new Position(0, 0));
			players.put(PlayerColor.Red, redPlayer);
			
			Player greenPlayer = new StandardPlayer(PlayerColor.Green);
			((StandardPlayer)greenPlayer).setSettlement(new Position(0, 6));
			players.put(PlayerColor.Green, greenPlayer);
			
			Player bluePlayer = new StandardPlayer(PlayerColor.Blue);
			((StandardPlayer)bluePlayer).setSettlement(new Position(6, 0));
			players.put(PlayerColor.Blue, bluePlayer);
			
			Player yellowPlayer = new StandardPlayer(PlayerColor.Yellow);
			((StandardPlayer)yellowPlayer).setSettlement(new Position(6, 6));
			players.put(PlayerColor.Yellow, yellowPlayer);
			
			// setup rounds left (25 - the one about to take place)
			gd.setRoundsLeft(gameContext.getFactory().getGameRoundsStrategy().getRoundsToPlay() - 1);
			
			//Set the starting player to None. We Need to throw Dice before we know the player
			gd.setPlayerInTurn(PlayerColor.None);
			System.out.println("Player in newGame: " + gameContext.getPlayerInTurn());
			gameStarted = true; //marker at spillet er startet
		}
	}

	public void rollDie() throws IllegalActionException {
		if(gameStarted)
		{
			System.out.println("Player before in turn: " + gameContext.getPlayerInTurn());
			
			GameObjectServer objServer = gameContext.getGameObjectServer();
			int dieValue = objServer.getDiceRollStrategy().rollDie();
			objServer.getGameData().setLastDieRoll(dieValue);
			TurnSequenceStrategy tss = objServer.getTurnSequenceStrategy();
			tss.startNewTurn(objServer.getGameData().getLastDieRoll());
			dieThrown = true;
			
			//Debug values:
			System.out.println("Last die: " + objServer.getGameData().getLastDieRoll());
			System.out.println("Player in turn: " + gameContext.getPlayerInTurn());
		}
	}

	public PlayerColor turnCard() throws IllegalActionException {
		PlayerColor retval = gameContext.getPlayerInTurn().getColor();
		if(dieThrown)
		{	
			gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
			
			System.out.println("Player is" + gameContext.getPlayerInTurn());
			PlayerColor nextPlayer = gameContext.getGameObjectServer().getTurnSequenceStrategy().nextPlayer();
			System.out.println("Player is" + nextPlayer);
			retval = gameContext.getGameObjectServer().getGameData().setPlayerInTurn(nextPlayer);
		}
		return retval;
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public void yieldAttack() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
}
